<template>
  <a-spin :spinning="loading">
    <div class="simple-mat-list">
      <template v-if="dataSource.length > 0">
        <a-row :gutter="gutter">
          <a-col :span="span" v-for="imgItem in dataSource" :key="imgItem.id">
            <a-card
              hoverable
              class="simple-mat-list__material-item"
              :class="{
                'simple-mat-list__material-item--active': violationIds.has(
                  imgItem.id
                ),
              }"
              size="small"
            >
              <template #cover>
                <div
                  class="simple-mat-list__img-item"
                  @click.right.prevent="selectViolation(imgItem)"
                >
                  <a-image alt="example" :src="imgItem.image" />
                  <div
                    class="simple-mat-list__img-cover"
                    v-if="violationIds.has(imgItem.id)"
                  >
                    {{ selectText }}
                  </div>
                </div>
              </template>
              <a-card-meta>
                <template #title>
                  <a-tooltip>
                    <template v-if="imgItem.title" #title>
                      {{ imgItem.title }}
                    </template>
                    {{ imgItem.title || '暂无标题' }}
                  </a-tooltip>
                </template>
                <template #description>
                  <a-tooltip>
                    <template v-if="imgItem.description" #title>
                      {{ imgItem.description }}
                    </template>
                    {{ imgItem.description || '暂无描述' }}
                  </a-tooltip>
                </template>
              </a-card-meta>
            </a-card>
          </a-col>
        </a-row>
      </template>
      <a-empty v-else />
    </div>
  </a-spin>
</template>

<script setup lang="ts">
import type { MaterialItem } from '../MaterialList/types'

const props = withDefaults(
  defineProps<{
    dataSource: MaterialItem[]
    violationIds: Set<string>
    loading: boolean
    gutter?: [number, number]
    span?: number
    selectText?: string
  }>(),
  {
    gutter: () => [8, 8],
    span: 3,
    selectText: '💘',
  }
)

const emits = defineEmits<{
  (e: 'update:violationIds', violationIds: Set<string>): void
}>()

const selectViolation = (materialItem: MaterialItem) => {
  const { id } = materialItem
  const ids = new Set<string>([...props.violationIds])
  if (props.violationIds.has(id)) {
    ids.delete(id)
  } else {
    ids.add(id)
  }
  emits('update:violationIds', ids)
}
</script>

<style lang="scss" scoped>
@include b(simple-mat-list) {
  padding: 16px 0px;
  @include e(material-item) {
    width: 100%;
    @include m(active) {
      border: 2px #40a9ff solid !important;
    }
  }
  @include e(img-item) {
    text-align: center;
    position: relative;
  }
  @include e(img-cover) {
    position: absolute;
    top: 0px;
    left: 0px;
    height: 100%;
    width: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    color: rgba(255, 0, 0, 0.5);
    font-weight: 600;
    font-size: 65px;
  }
}
</style>
<style lang="scss">
.simple-mat-list {
  .ant-card {
    line-height: 1;
  }
  .ant-card-meta-title {
    font-size: 14px;
    line-height: 16px;
  }
  .ant-card-meta-description {
    font-size: 12px;
    line-height: 16px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
  }
  .ant-card-cover {
    height: 100%;
  }
  .ant-card-body {
    border-top: 1px solid #eee;
  }
  .ant-image {
    height: 200px;
    img {
      object-fit: contain;
    }
  }
  .ant-card-bordered {
    border: 1px solid #cccccc;
  }
}
</style>
